package com.demo.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.HttpKit;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.api.ApiResult;
import jodd.util.StringUtil;

public class Temaple {
	/**
	 * 模板消息接口
	 */
	private static String tempurl="https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=";
	private static String addTemp="https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=";
	private static String sendTemp="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
	private static String validIfo="https://api.weixin.qq.com/cgi-bin/user/info?access_token=";
	/**
	 * 模板消息接口调用请求
	 */
	public static ApiResult setIndustry(String jsonStr) {
		String jsonResult = HttpKit.post(tempurl +  AccessTokenApi.getAccessToken().getAccessToken(), jsonStr);
		return new ApiResult(jsonResult);
	}
	
	/**
	 * 获得模板ID
	 *POST数据示例如下：
   *  {
   *       "template_id_short":"TM00015"
   *  }
	 */
	public static ApiResult addTemplate(String jsonStr) {
		String jsonResult = HttpKit.post(addTemp + AccessTokenApi.getAccessToken().getAccessToken(), jsonStr);
		return new ApiResult(jsonResult);
	}
		
	/**
	 * 发送模板消息
	 */
	public static ApiResult sendTemp(String jsonStr) {
		String jsonResult = HttpKit.post(sendTemp + AccessTokenApi.getAccessToken().getAccessToken(), jsonStr);
		return new ApiResult(jsonResult);
	}

	/**
	 * 用户鉴权
	 */
	public static boolean isSubscribe(String openid){

		String jsonResult = HttpKit.post(validIfo + AccessTokenApi.getAccessToken().getAccessToken()+ "&openid=" + openid + "&lang=zh_CN", "");
		JSONObject jsonRes = JSON.parseObject(jsonResult);
		return 1 == jsonRes.getIntValue("subscribe");
	}
	
}
